if(document.getElementById("ainuo_toggle_top")){
	$(window).scroll(function() { 
		var yheight=getScrollTop();
		if(yheight > 200){
			$("#ainuo_toggle_top").fadeIn();
		}else{
			$("#ainuo_toggle_top").fadeOut();
		}
	})
}
function getScrollTop() {  
	var scrollPos;  
	if (window.pageYOffset) {  
	scrollPos = window.pageYOffset; }  
	else if (document.compatMode && document.compatMode != 'BackCompat')  
	{ scrollPos = document.documentElement.scrollTop; }  
	else if (document.body) { scrollPos = document.body.scrollTop; }   
	return scrollPos;   
}  
if($('div.pg').length > 0) {
	ainuopage.converthtml();
}
if($('.popup').length > 0) {
	popup.init();
}

$('body').bind('touchstart',function(e){
	  startX = e.originalEvent.changedTouches[0].pageX;
	  startY = e.originalEvent.changedTouches[0].pageY;
  });
$("body").bind("touchmove",function(e){
	//获取滑动屏幕时的X,Y
	endX = e.originalEvent.changedTouches[0].pageX;
	endY = e.originalEvent.changedTouches[0].pageY;
	//获取滑动距离
	distanceX = endX-startX;
	distanceY = endY-startY;
	//判断滑动方向
	if(Math.abs(distanceX)<Math.abs(distanceY) && distanceY<0){
		$("#ainuo_quick_bot").fadeOut();
	} else if(Math.abs(distanceX)<Math.abs(distanceY) && distanceY>0){
		$("#ainuo_quick_bot").fadeIn();
	}
})
		
		//底部发帖
		$('.botpost').on('click', function(){
			$("#apostmask").fadeIn();
		});
		$(".aclose").click(function() {
			$("#apostmask").fadeOut();
		});		
		//左侧导航
		$("#ainuo_toggle_menu").click(function(){
			$("#mm-menu").addClass("active");
			$("#menu-mask").addClass("active");
		});	
		$("#ainuo_toggle_menuguide").click(function(){
			$("#mm-menu").addClass("active");
			$("#menu-mask").addClass("active");
		});	
		$("#menu-mask").click(function(){
			$("#mm-menu").removeClass("active");
			$("#menu-mask").removeClass("active");
		});	
	
		//子版块
		$('.asubforum').on('click', function(event){
            event.preventDefault();
            $('.subname_list').addClass('is-visible');
        });
		
        $('.subname_list').on('click', function(event){
                event.preventDefault();
                $(this).removeClass('is-visible');
        });
		

		
		
//回复
$('.fastcon_a').on('click', function(event){
	event.preventDefault();
	$('.afastreply').addClass('is-visible');
	$("#afastreply").find("textarea").focus();
});
$('.ainuobotsortreply_a').on('click', function(event){
	event.preventDefault();
	$('.afastreply').addClass('is-visible');
	$("#afastreply").find("textarea").focus();
});
$('.acthuifu').on('click', function(event){
	event.preventDefault();
	$('.afastreply').addClass('is-visible');
	$("#afastreply").find("textarea").focus();
});

$('.cenclepl').on('click', function(event){
		event.preventDefault();
		$('.afastreply').removeClass('is-visible');
});
		
		//发视频
		$('.psvideo').on('click', function(event){
            event.preventDefault();
            $('.ainuo_postvideo').addClass('is-visible');
        });
		$('.cancelupvodeo').on('click', function(event){
			event.preventDefault();
			$('.ainuo_postvideo').removeClass('is-visible');
        });
		
		
		//活动
		$('.acttoggle_join').on('click', function(event){
            event.preventDefault();
            $('.activityjoin').addClass('is-visible');
        });
		$('.acttoggle_cancel').on('click', function(event){
            event.preventDefault();
            $('.activityjoincancel').addClass('is-visible');
        });
		$('.cancelbaoming').on('click', function(event){
            event.preventDefault();
            $('.activityjoincancel').removeClass('is-visible');
			$('.activityjoin').removeClass('is-visible');
        });
		
		//发音频
		$('.psmusic').on('click', function(event){
            event.preventDefault();
            $('.ainuo_postmusic').addClass('is-visible');
			$("#ainuo_postmusic").find("textarea").focus();
        });
		$('.cancelupmusic').on('click', function(event){
			event.preventDefault();
			$('.ainuo_postmusic').removeClass('is-visible');
        });
		
		//发视频链接
		$('.psvideolink').on('click', function(event){
            event.preventDefault();
            $('.ainuo_postvideolink').addClass('is-visible');
			$("#ainuo_postvideolink").find("textarea").focus();
        });
		$('.canceluplink').on('click', function(event){
			event.preventDefault();
			$('.ainuo_postvideolink').removeClass('is-visible');
        });
		
		
		

		$(".ainuolazyloadbg").lazyload({
			effect: "fadeIn",
			placeholder     : "template/qu_app/touch/style/lazy/lazy.png",
			threshold : 180,
			//failurelimit : 6,
			//container: $("#ainuo_contop"),
		});
		$(".ainuolazyload").lazyload({
			effect: "fadeIn",
			placeholder     : "template/qu_app/touch/style/lazy/lazy.png",
			threshold : 180,
			//failurelimit : 6,
			//container: $("#ainuo_contop"),
		});
		
		$("#ainuo_toggle_top").click(function(){
			$('body').animate({scrollTop:0},300);
			return false;
		});
/*		$('.ainuo_right_more').on('click', function() {
			var botMenu = document.getElementById("ainuo_quick_bot").style.right;
			if(botMenu == '10px'){
				$('.ainuo_quick_bot').animate({right:'-40px'},200);
				document.getElementById("ainuo_right_more").innerHTML = '<i class="iconfont icon-more"></i>';
				document.getElementById("ainuo_right_more").style.background = '#444';
			}else{
				$('.ainuo_quick_bot').animate({right:'10px'},200);
				document.getElementById("ainuo_right_more").innerHTML = '<i class="iconfont icon-close"></i>';
				document.getElementById("ainuo_right_more").style.background = '#000';
			}
		});
		$('.ainuo_quick_bot a').on('click', function() {
			$('.ainuo_quick_bot').animate({right:'-40px'},200);
			document.getElementById("ainuo_right_more").innerHTML = '<i class="iconfont icon-more"></i>';
			document.getElementById("ainuo_right_more").style.background = '#444';
		});*/
		

		$('.plc .pi img').each(function () {
			AutoResizeImage(this);
		});
		function AutoResizeImage(objImg) {
			var img = new Image();
			img.src = objImg.src;
			img.onload = function(){
				if(this.width >= 200){
					$(objImg).css('width','100%');
					$(objImg).css('height','auto');
				}
			}
		}
		
		

		//share
		$('.ashare').on('click', function() {
			if(isWeiXin() || (navigator.userAgent.indexOf('MQQBrowser') > -1)){
				$("#ainuowexopen").fadeIn();
				return;
			}else if((navigator.userAgent.indexOf('Safari') > -1) && (navigator.userAgent.indexOf('iPhone') > -1)){
				$("#ainuowexopen_safari").fadeIn();
				return;
			}else{
				soshm.popIn({
					url: sharelink,
					title: sharetitle,
					digest: sharedesc,
					pic: shareicon,
					sites: ['weixin', 'weixintimeline', 'weibo', 'qq', 'qzone','yixin']
				});
			}
		});
		$('.ainuowxfenxiang').on('click', function() {
			$("#ainuowexopen_safari").fadeOut();
			$("#ainuowexopen").fadeOut();
			return;
		});
		
		function isWeiXin(){
			var ua = window.navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger/i) == 'micromessenger'){
				return true;
			}else{
				return false;
			}
		};
			
		


			var ainuos_sright_Text;
			(function($){
				$.fn.extend({
					
					ainuo_insert: function(a){
						var $ainuos=$(this)[0];
						if ($ainuos.selectionStart || $ainuos.selectionStart == '0') {
							var startPos = $ainuos.selectionStart;
							var endPos = $ainuos.selectionEnd;
							$ainuos.value = $ainuos.value.substring(0, startPos) + a + $ainuos.value.substring(endPos, $ainuos.value.length);
							$ainuos.selectionStart = $ainuos.selectionEnd = startPos + a.length;
						}else {
							this.value += a;
						}
						$ainuos.focus();
					},
					ainuo_delete: function(){
						var $ainuos=$(this)[0];
						var is_smilies = -1;
						if ($ainuos.selectionStart && ($ainuos.selectionStart == $ainuos.selectionEnd)) {
							var startPos = $ainuos.selectionStart;
							var startText = $ainuos.value.substring(0, startPos);
							var endText = $ainuos.value.substring(startPos, $ainuos.value.length);
							var ainuos_smilies_len = 0, delStart = 0, ainuos_startText_lendata = '';
							for(i in ainuo_smilies_array) {
								ainuos_smilies_len = ainuo_smilies_array[i][0].length;
								ainuos_startText_lendata = startText.substring(startText.length - ainuos_smilies_len, startText.length);
								if(in_array(ainuos_startText_lendata, ainuo_smilies_array[i])){
									delStart = startText.length - ainuos_smilies_len;
									$ainuos.value = startText.substring(0, delStart)  + endText;
									$ainuos.selectionStart = $ainuos.selectionEnd = delStart;
									is_smilies = 1;
									break;
								}
							}
							if(is_smilies == 1){
								return false;
							}else{
								var ainuos_sall_Text = ainuos_sleft_Text = ainuos_sright_Text = '';
								var ainuos_smilies_len = ainuos_sleft_length = 0;
								for(o in ainuo_smilies_array) {
									ainuos_smilies_len = ainuo_smilies_array[o][0].length;
									ainuos_sleft_Text = $ainuos.value.substring(startPos - ainuos_smilies_len + 1, startPos);
									ainuos_sright_Text = $ainuos.value.substring(startPos, startPos + ainuos_smilies_len - 1);
									ainuos_sleft_length = ainuos_sleft_Text.length;
									ainuos_sall_Text = ainuos_sleft_Text + ainuos_sright_Text;
									for(p in ainuo_smilies_array[o]) {
										is_smilies = ainuos_sall_Text.indexOf(ainuo_smilies_array[o][p]);
										if(is_smilies >= 0){
											var startText = $ainuos.value.substring(0, startPos - ainuos_sleft_length + is_smilies);
											var endText = $ainuos.value.substring(startPos - ainuos_sleft_length + is_smilies + ainuos_smilies_len, $ainuos.value.length);
											$ainuos.value = startText + endText;
											$ainuos.selectionStart = $ainuos.selectionEnd = startText.length;	
											return false;
										}
									}
								}
							}
						}
					}
				})
			})(jQuery);


/* ===============================================================================
************   Tabs   ************
=============================================================================== */


    var showTab = function (tab, tabLink, force) {
        var newTab = $(tab);
        if (arguments.length === 2) {
            if (typeof tabLink === 'boolean') {
                force = tabLink;
            }
        }
        if (newTab.length === 0) return false;
        if (newTab.hasClass('active')) {
            if (force) newTab.trigger('show');
            return false;
        }
        var tabs = newTab.parent('.tabs');
        if (tabs.length === 0) return false;

        // Remove active class from old tabs
        var oldTab = tabs.children('.tab.active').removeClass('active');
        // Add active class to new tab
        newTab.addClass('active');
        // Trigger 'show' event on new tab
        newTab.trigger('show');

        // Update navbars in new tab
        /*if (!isAnimatedTabs && newTab.find('.navbar').length > 0) {
        // Find tab's view
        var viewContainer;
        if (newTab.hasClass(app.params.viewClass)) viewContainer = newTab[0];
        else viewContainer = newTab.parents('.' + app.params.viewClass)[0];
        app.sizeNavbars(viewContainer);
        }*/

        // Find related link for new tab
        if (tabLink) tabLink = $(tabLink);
        else {
            // Search by id
            if (typeof tab === 'string') tabLink = $('.tab-link[href="' + tab + '"]');
            else tabLink = $('.tab-link[href="#' + newTab.attr('id') + '"]');
            // Search by data-tab
            if (!tabLink || tabLink && tabLink.length === 0) {
                $('[data-tab]').each(function () {
                    if (newTab.is($(this).attr('data-tab'))) tabLink = $(this);
                });
            }
        }
        if (tabLink.length === 0) return;

        // Find related link for old tab
        var oldTabLink;
        if (oldTab && oldTab.length > 0) {
            // Search by id
            var oldTabId = oldTab.attr('id');
            if (oldTabId) oldTabLink = $('.tab-link[href="#' + oldTabId + '"]');
            // Search by data-tab
            if (!oldTabLink || oldTabLink && oldTabLink.length === 0) {
                $('[data-tab]').each(function () {
                    if (oldTab.is($(this).attr('data-tab'))) oldTabLink = $(this);
                });
            }
        }

        // Update links' classes
        if (tabLink && tabLink.length > 0) tabLink.addClass('active');
        if (oldTabLink && oldTabLink.length > 0) oldTabLink.removeClass('active');
        tabLink.trigger('active');

        //app.refreshScroller();

        return true;
    };

    var old = $.showTab;
    $.showTab = showTab;

    $.showTab.noConflict = function () {
        $.showTab = old;
        return this;
    };
    $(document).on("click", ".tab-link", function(e) {
        e.preventDefault();
        var clicked = $(this);
        showTab(clicked.data("tab") || clicked.attr('href'), clicked);
    });


		
